<?php

namespace App\Console\Commands;

use App\Models\Base\ServerList;
use App\Models\Base\StatResource;
use App\Models\Base\StatResourcesCollect;
use App\Service\ResourceService;
use Illuminate\Support\Facades\Log;

class FetchResource extends CommandBase
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'fetch:resource';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '资源消耗统计';

    /**
     * 需要统计的资源类型:金币、元宝、仙晶、灵石.
     *
     * @var array
     */
    protected $types;

    /**
     * Create a new command instance.
     */
    public function __construct()
    {
        parent::__construct();
        $types = config('game.moneyType');
        $this->types = array_slice(array_values($types), 0, 4);
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $servers = ServerList::pluck('server_id');
        //资源消耗
        foreach ($servers as $server_id) {
            setDB($server_id);
            //获取消耗资源
            $logs = ResourceService::getResourceData($this->types, $this->yesterdayTimestamp, $this->todayTimestamp);
            //获取当前资源总库存
            $inventorys = ResourceService::getInventory();
            //写入资源汇总表
            $this->insertResourceCollect($server_id, $inventorys[0], $logs);
            //玩家获得资源数据
            $gets = ResourceService::getResourceData($this->types, $this->yesterdayTimestamp, $this->todayTimestamp, false);
            if (!$logs->isEmpty()) {
                //写入资源消耗记录表
                $this->insertResource($logs, $server_id);
            }
            if (!$gets->isEmpty()) {
                //记录资源获取记录
                $this->insertResource($gets, $server_id, 2);
            }
        }
    }

    /**
     * [insertResource 写入资源变动记录表].
     *
     * @Author   王文凡
     * @DateTime 2018-05-31
     *
     * @version  1.0
     *
     * @param array $datas [资源数据]
     * @param int   $way   [类型1消耗,2获取]
     */
    protected function insertResource($datas, $server_id, $way = 1)
    {
        foreach ($datas as $item) {
            $data['server_id'] = $server_id;
            $data['date_time'] = $this->yesterday;
            $data['resource_type'] = $item->moneytype;
            $data['change_type'] = $item->changetype;
            $data['number'] = abs($item->counts);
            $data['way_type'] = $way;
            if (false == StatResource::create($data)) {
                Log::error('写入资源数据失败:', $data);
            }
        }
    }

    /**
     * [insertResourceCollect 写入资源汇总].
     *
     * @Author   王文凡
     * @DateTime 2018-05-31
     *
     * @version  1.0
     *
     * @param [type] $inventorys [description]
     * @param [type] $logs       [description]
     */
    protected function insertResourceCollect($server_id, $inventorys, $logs)
    {
        $log_number = 0;
        //资源消耗汇总
        $coins = $golds = $treasures = $stones = 0;
        foreach ($logs as $key => $value) {
            $number = abs($value->counts);
            switch ($value->moneytype) {
                case $this->types[0]: //金币
                    $coins += $number;
                    break;
                case $this->types[1]: //元宝
                    $golds += $number;
                    break;
                case $this->types[2]: //仙晶
                    $treasures += $number;
                    break;
                case $this->types[3]: //灵石
                    $stones += $number;
                    break;
            }
        }
        //库存、消耗、总数、占比
        $inventory = $quantity = $totality = $proportion = 0;
        foreach ($this->types as $key => $type) {
            if (0 == $key) {
                $inventory = $inventorys->coins;
                $quantity = $coins;
            } elseif (1 == $key) {
                $inventory = $inventorys->golds;
                $quantity = $golds;
            } elseif (2 == $key) {
                $inventory = $inventorys->treasures;
                $quantity = $treasures;
            } elseif (3 == $key) {
                $inventory = $inventorys->stones;
                $quantity = $stones;
            }
            $totality = $inventory + $quantity;
            if ($totality > 0) {
                $proportion = $quantity / $totality;
            }
            $data = [
                'type' => $type,
                'server_id' => $server_id,
                'date_time' => $this->yesterday,
                'inventory' => $inventory,
                'quantity' => $quantity,
                'proportion' => $proportion,
                'totality' => $totality,
            ];
            //写入数据
            if (StatResourcesCollect::create($data)) {
                ++$log_number;
            } else {
                Log::error('写入资源汇总数据失败:', $data);
            }
        }
        Log::debug('成功写入资源汇总数据', ['number' => $log_number]);
    }
}
